using UnityEngine;
using UnityEngine.Events;
namespace DefaultNamespace.Skill
{
    public class HealthSystem : MonoBehaviour
    {
        [Header("Settings")]
        [SerializeField]
        private float maxHealth = 100f;
        [SerializeField]
        private bool invulnerable = false;
        [Header("Events")]
        public UnityEvent onDamageTaken;
        public UnityEvent onHealed;
        public UnityEvent onDeath;
        public UnityEvent<float> onHealthChanged; // Passes current health percentage
        private float currentHealth;
        private bool isDead = false;
        private void Awake()
        {
            currentHealth = maxHealth;
        }
        public void TakeDamage(float amount)
        {
            if (invulnerable || isDead) return;
            currentHealth = Mathf.Max(currentHealth - amount, 0);
            onHealthChanged?.Invoke(GetHealthPercentage());
            onDamageTaken?.Invoke();
            if (currentHealth <= 0)
            {
                Die();
            }
        }
        public void Heal(float amount)
        {
            if (isDead) return;
            currentHealth = Mathf.Min(currentHealth + amount, maxHealth);
            onHealthChanged?.Invoke(GetHealthPercentage());
            onHealed?.Invoke();
        }
        public void RestoreFullHealth()
        {
            currentHealth = maxHealth;
            onHealthChanged?.Invoke(1f);
            onHealed?.Invoke();
        }
        private void Die()
        {
            isDead = true;
            onDeath?.Invoke();
        }
        public void Revive(float healthPercentage = 0.5f)
        {
            isDead = false;
            currentHealth = maxHealth * Mathf.Clamp01(healthPercentage);
            onHealthChanged?.Invoke(GetHealthPercentage());
        }
        public float GetCurrentHealth() => currentHealth;
        public float GetMaxHealth() => maxHealth;
        public float GetHealthPercentage() => currentHealth / maxHealth;
        public bool IsDead() => isDead;
        public void SetInvulnerable(bool state) => invulnerable = state;
    }
}